/**
 * @(#)Data.java
 *
 * Copyright: Copyright (c) 2003 Carnegie Mellon University
 *
 * This program is the implementation of the original system in the Assignment 2 of "Architectures
 * of Software Systems" class. For the details of each classes, please refer to the provided javadoc
 * documents. You will also find some useful information such as implementation decisions, coding
 * standards, and naming conventions in the README file.
 *
 * History:
 *
 * Date          Author              Change
 * ----------    ----------------    ---------------------------------------------------------------
 * 07/15/2003    Jung Soo Kim        Initial version.
 */

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;




/**
 * A data node provides access to student and course data including reading the record information
 * and writing registration information. Note that current version of data node doesn't support
 * writing student and course records to files. A shutdown means loss of all registration
 * information.
 */
public class Log extends UnicastRemoteObject implements RILog {

    private static final String LOG_NAME = "Log";

    private String sLogFileName;
    OutputStreamWriter fLog ; 
    
    /**
     * Construct a data node that provides access to student and course data. Initial data are
     * filled with the records in the given student and course record files. At the time of
     * creation, the data node does not contain any registration information.
     *
     * @param sStudentFileName the name of student record file
     * @param sCourseFileName the name of course record file
     */
    public Log(String sFileName)
           throws RemoteException, FileNotFoundException, IOException {
        // Open the given student and course files.
        sLogFileName = sFileName;
        
        fLog = new OutputStreamWriter(new FileOutputStream(sLogFileName, true
        		));
        
    }

    

    /**
     * Make a registration. No conflict check is done before updating the database. Nothing happens
     * if there is no matching student record and/or course record.
     *
     * @param  sSID a student ID to register
     * @param  sCID a course ID to register
     * @param  sSection a course section to register
     */
    public void writeLog(String sID, String sCommand, String sStamp) {
    	 
	   try
	   {
		   
		   String sLog = "UserID:"+ sID +" CommandType:" + sCommand + " TimeStamp:" + sStamp + "\n";
		  
		   fLog.append(sLog);
		   
		   
		   System.out.print(sLog);
		   fLog.flush(); 
		   
	   }
	   catch (IOException e)
	   {
		   
	   }
    }

    /**
     * Creates a data node and starts it. Three parameters are expected, the first one as student
     * file name, the second one as course file name, and the third one as data node name.
     *
     * @param args array of input parameters
     */
    public static void main(String args[]) {

        try {
            // Create a data node and make it available through RMI.
            Log objData = new Log("Log.txt");
            Naming.bind(LOG_NAME, objData);
            System.out.println("Log node is ready to serve.");

            // Wait for user interrupt.
            System.out.println("Press Enter to terminate.");
            System.in.read();

            // Clean up and finish.
            Naming.unbind(LOG_NAME);
            System.out.println("Data node is leaving, bye. Press ctrl-c if the delay is too long.");
        }
        catch (java.rmi.ConnectException e) {
            // Show error message and quit.
            System.err.println("Java RMI error: check if rmiregistry is running.");
            System.exit(1);
        }
        catch (Exception e) {
            // Dump the exception information for debugging.
            System.err.println("Unexpected exception at " + LOG_NAME);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
